package com.xzlf.service;

import com.xzlf.dto.ResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class SyncSendService {

    @Autowired
    private DemoService demoService;

    public ResultDTO send(int count) {
        ResultDTO rt =  null;
        String name = Thread.currentThread().getName();
        try {
            rt = new ResultDTO();
            rt.setImgCount(count);
            rt.setMsg(MessageFormat.format("完成{0}张图片的tongbu请求", count));
            demoService.add(name);
            log.info("=========同步线程--{}--启动，各位让让===========", name);
            while (count > 0) {
                log.info("---------{}--发送同步图片：{}", name, count);
                TimeUnit.SECONDS.sleep(1);
                count--;
            }
            rt.setResponseDate(new Date());
            rt.setResult("success");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            demoService.pop();
        }
        return rt;
    }
}
